Component({
  properties: {
    post: {
      type: Object,
      value: {}
    },
    showUserInfo: {
      type: Boolean,
      value: true
    },
    showActions: {
      type: Boolean,
      value: true
    },
    showBookInfo: {
      type: Boolean,
      value: true
    }
  },

  data: {
    isLiked: false,
    isCollected: false,
    likeCount: 0,
    collectCount: 0,
    commentCount: 0
  },

  lifetimes: {
    attached() {
      this.initData();
    }
  },

  observers: {
    'post': function(post) {
      if (post) {
        this.initData();
      }
    }
  },

  methods: {
    /**
     * 初始化组件数据
     */
    initData() {
      const { post } = this.data;
      if (!post) return;

      this.setData({
        isLiked: post.isLiked || false,
        isCollected: post.isCollected || post.isFavorited || false,
        likeCount: post.likeCount || post.likes || 0,
        collectCount: post.favoriteCount || post.collectCount || post.collects || 0,
        commentCount: post.commentCount || post.comments || 0
      });
    },

    /**
     * 点击书摘卡片
     */
    onCardTap() {
      const { post } = this.data;
      this.triggerEvent('cardtap', { post });
    },

    /**
     * 点击用户头像
     */
    onUserTap() {
      const { post } = this.data;
      this.triggerEvent('usertap', { userId: post.userId });
    },

    /**
     * 点击书籍信息
     */
    onBookTap() {
      const { post } = this.data;
      this.triggerEvent('booktap', { bookId: post.bookId });
    },

    /**
     * 点赞
     */
    onLike() {
      const { post, isLiked, likeCount } = this.data;
      
      // 立即更新UI
      this.setData({
        isLiked: !isLiked,
        likeCount: isLiked ? likeCount - 1 : likeCount + 1
      });

      // 触发事件
      this.triggerEvent('like', { 
        postId: post.id, 
        isLiked: !isLiked 
      });
    },

    /**
     * 收藏
     */
    onCollect() {
      const { post, isCollected, collectCount } = this.data;
      
      // 立即更新UI
      this.setData({
        isCollected: !isCollected,
        collectCount: isCollected ? collectCount - 1 : collectCount + 1
      });

      // 触发事件
      this.triggerEvent('collect', { 
        postId: post.id, 
        isCollected: !isCollected 
      });
    },

    /**
     * 评论
     */
    onComment() {
      const { post } = this.data;
      this.triggerEvent('comment', { postId: post.id });
    },

    /**
     * 分享
     */
    onShare() {
      const { post } = this.data;
      this.triggerEvent('share', { post });
    },

    /**
     * 预览图片
     */
    onImageTap(e) {
      const { index } = e.currentTarget.dataset;
      const { post } = this.data;
      
      wx.previewImage({
        current: post.images[index],
        urls: post.images
      });
    }
  }
});
